
import presetVar from './presetVar'
import { high } from 'nc-lightapp-front'
import  requestApi from './requestApi'
import "./index.less"
const { Refer } = high

const RULETYPE = {
    AVERAGE:1,  //平均分配
    RATIO: 2, //按比例分配
    AMOUNT:3  //按金额分配
}

export default function () {
    this.props.createUIDom({
        appcode: '201102BCLF',
        pagecode: '201102KSFT_XZFTGZ'
    }, (data) => {
        const meta = data.template
        const props = this.props
        const that = this
        meta[presetVar.headCode].items.forEach(item => {
            if(item.attrcode === 'shareRule') {
                item.render = () => {
                    let multiLang = this.props.MutiInit.getIntl(2011);
                    let ruleCode = multiLang.get("201103INUP-0048");
                    let ruleName = multiLang.get("201103INUP-0049");
                    let ruleType = multiLang.get("201103INUP-0050");
                    let pkGroup = multiLang.get("201103INUP-0051");
                    let ruleobjName = multiLang.get("201103INUP-0052");
                    let pkOrg = multiLang.get("201103INUP-0053");
                    let tabData = [];
                    tabData.push(ruleCode);
                    tabData.push(ruleName);
                    tabData.push(ruleType);
                    tabData.push(pkGroup);
                    tabData.push(ruleobjName);
                    tabData.push(pkOrg);
                    const columnConfig = [ { name: tabData,
                        code: ['rule_code', 'rule_name', 'rule_type', 'pk_group', 'ruleobj_name', 'pk_org']}]
                    return <Refer
                                refType="grid"
                                refName={ multiLang && multiLang.get('201103INUP-0047')}
                                columnConfig={columnConfig}
                                queryGridUrl={'/nccloud/erm/ersetting/RapidShareRuleQueryAction.do'}
                                queryCondition={() => getQueryConditon(props)}
                                onChange={(value) => {onChangeRule.call(that, props, value)} }
                                needFocusOnClose={false}//关掉弹框无需获取焦点，否则有种直接点击确认按钮不起作用的错觉
                                popWindowClassName='quickShareRefZindex'//解决分摊规则参照弹框z-index小于快速分摊弹框问题
                                allowSearchConfig={false}//不显示搜索设置
                            />
                }
            }
        })
        this.props.meta.setMeta(meta)
    })
}

function getQueryConditon(props) {
    const { pk_org } = props.getQuickShareParams()
    return {pk_org: pk_org.value}
}

export function onChangeRule(props, value) {
    const { pk_org } = props.getQuickShareParams()
    let pk = value ? value.refpk : ''
    this.selectRulePk = pk
    const reqParam = {
            pagecode: "201101FTGZZ_card", 
            pk, 
            pk_org: pk_org.value
    }
    requestApi.getTableData({data:reqParam}).then(res => {
        const showPk = []
        const ruleType = res.data.head.rule_code.rows[0].values.rule_type.value
        const ruleName = res.data.head.rule_code.rows[0].values.ruleobj_name.value.split('#')[1]
        if(ruleType == RULETYPE.RATIO) {
            showPk.push(ruleName,'share_ratio')
        } else if(ruleType == RULETYPE.AMOUNT) {
            showPk.push('assume_amount', ruleName)
        } else {
            showPk.push(ruleName)
        }

        //重新设置模板
        const meta = props.meta.getMeta()
        meta[presetVar.bodyCode].items.forEach(item => {
            if(showPk.includes(item.attrcode)) {
                props.table.showColByKey(presetVar.bodyCode, item.attrcode)
            } else {
                props.table.hideColByKey(presetVar.bodyCode, item.attrcode)
            }
        })
        window.ecshare_rule_data = res.data.body.rule_data
        props.table.setAllTableData(presetVar.bodyCode, res.data.body.rule_data)
    })
}